package ru.mcfr.oxygen.updater;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: wstarcev
 * Date: 16.04.11
 * Time: 0:31
 * To change this template use File | Settings | File Templates.
 */
public class Version implements Comparable {

    public Vector<Integer> info = null;

    public Version(String ver){
        info = new Vector<Integer>();
        String[] split = ver.split("\\.");
        for (int i = 0; i < split.length; i++)
            try{
                String part = split[i];
                Integer value = Integer.valueOf(part);
                info.add(i, value);
            } catch (Exception e){
                System.err.println(e.getLocalizedMessage());
            }

    }

    public static Version fromString(String src){
        Version ver = new Version(src);
        if (ver.info.size() > 0)
            return ver;
        else return null;
    }

    public static Version fromDir(String path){
        if (!path.endsWith(File.separator))
            path += File.separator;
        File vf = new File(path + "version.txt");
        if (vf.exists()){
            try {
                FileReader fr = new FileReader(vf);
                char[] buf = new char[1];
                try {
                    String version = "";

                    while (fr.read(buf) > 0)
                        if (!String.valueOf(buf).equals("|"))
                            version += String.valueOf(buf);
                        else break;

                    return new Version(version);
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return null;
            }
        } else
            return null;
    }

    public static Version fromPath(String path){
        for (String name : path.split(File.separator)){
            Version thisVer = Version.fromString(name);
            if (thisVer != null)
                return thisVer;
        }
        return null;
    }

    public int compareTo(Object obj) {
        String version = "";
        if (obj instanceof String)
            version = (String) obj;
        else if (obj instanceof Version)
            version = ((Version) obj).toString();
        else return 0;
        if (version.isEmpty()) return 1;
        Version newVer = new Version(version);
        for (int i = 0; i < info.size() & i < newVer.info.size(); i++)
            if (!info.get(i).equals(newVer.info.get(i)))
                return info.get(i).compareTo(newVer.info.get(i));
        if (info.size() > newVer.info.size())
            return 1;
        return 0;
    }

    public String toString(){
        String res = "";
        for (int i = 0; i < info.size(); i++)
            res += info.get(i) + ".";
        if (res.endsWith("."))
            res = res.substring(0, res.length() - 1);
        return  res;
    }
}
